package com.wpf.system.common.utils;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Component
public class RedisUtils {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    //存缓存
    public void set(String key ,String value,Long timeOut){ redisTemplate.opsForValue().set(key,value,timeOut, TimeUnit.SECONDS); }
    //取缓存
    public String get(String key){
        return (String) redisTemplate.opsForValue().get(key);
    }
    //清除缓存
    public void del(String key){ redisTemplate.delete(key); }

    //存入ZSet
    public void setZSet(String key,String value,double time){
        stringRedisTemplate.opsForZSet().add( key, value, time );
    }

    //获取延时队列
    public Set<ZSetOperations.TypedTuple<String>> getZSet(String key,double minTime,double maxTime){
        return stringRedisTemplate.opsForZSet().rangeByScoreWithScores(key,minTime,maxTime);
    }

    //删除延时任务
    public void delZSet(String key,String value){
        stringRedisTemplate.opsForZSet().remove(key,value);
    }

}
